<template>
  <div class="loginContaniner">
    <div class="loginbox">
      <!-- 头像区 -->
      <div class="avatarbox">
        <img src="../assets/heima.png" alt="" />
      </div>
      <!-- 登录表单区------------------------------------------------- -->
      <el-form
        ref="loginFormRef"
        :rules="loginFormRules"
        :model="loginForm"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户区------------------------------------------------- -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码区------------------------------------------------- -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!-- 按钮区------------------------------------------------- -->
        <div class="btns">
          <el-button type="primary" @click="dologin">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import "@/assets/fonts/iconfont.css";

//导入 登录接口
import { loginAPI } from "@/api";

export default {
  data() {
    return {
      //登录表单 数据对象
      loginForm: {
        username: "123",
        password: "ewqsdasd",
      },
      //登录表单验证对象
      loginFormRules: {
        //登录名 文本框 校验规则
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetForm() {
      //获取表单对象 调用 重置方法（还原数据并清空校验提示）
      this.$refs.loginFormRef.resetFields();
    },
    dologin() {
      ///再次校验 表单元素
      this.$refs.loginFormRef.validate(async (result) => {
        //如果校验为通过 则停止运行
        if (!result) return;
        //如果通过校验 则 获取表单数据 发送给 登录接口
        let loginFormData = { ...this.loginForm };
        let { data: resbody } = await loginAPI(loginFormData);
        this.$msg.warning(resbody.meta.msg);
        //如果失败停止执行
        if (resbody.meta.status !== 200) return;
        //如果成功 将返回token的值  保存到sessionStorage
        window.sessionStorage.setItem("token", resbody.data.token);
        //跳转到home组件
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.loginContaniner {
  background-color: #2b4b6b;
  height: 100%;
}

.loginbox {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatarbox {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
  }
}

// 表单样式
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
// 按钮区
.btns {
  display: flex;
  justify-content: flex-end;
  padding-bottom: 25px;
}
</style>